<?php
//延迟开奖
namespace App\Jobs\QiPao;

use App\Jobs\Job;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\Log;

class ScreenJob extends Job
{
    protected $data;
    const jobName = 'ScreenJob';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $data = $this->data;
        $activityId = $data['id']??0;
        try{
            if($activityId){
                $res = app('repo_screen')->openScreenActivity($data);
                Log::error('ScreenJob:' . json_encode([
                        'res' => $res,
                        'data' => $data,
                    ]));
                if($res['code'] != 200){
                    if(!empty($res['delay'])){
                        $screenJob = (new ScreenJob($data))->delay($res['delay']);
                        dispatch($screenJob);
                    }else{
                        //最大重试5次
                        $data['error_num'] = empty($data['error_num'])?1:$data['error_num']+1;
                        if($data['error_num'] <5){
                            $screenJob = (new ScreenJob($data))->delay($data['error_num']);
                            dispatch($screenJob);
                        }
                    }
                }
            }
        }catch (\Exception $e){
            //最大重试5次
            $data['error_num'] = empty($data['error_num'])?1:$data['error_num']+1;
            if($data['error_num'] <5){
                $screenJob = (new ScreenJob($data))->delay($data['error_num']);
                dispatch($screenJob);
            }
        }
    }

}
